package first_practice;

/**
 * @author ZhiWen Ren
 * @version 1.0
 * @description: LeetCode-240-搜索二维矩阵 II
 * @date 2025/3/31 16:44
 */
public class Solution240 {
    public boolean searchMatrix(int[][] matrix, int target) {
        boolean ans;
        for (int[] row : matrix) {
            ans = search(row, target);
            if (ans)
                return true;
        }
        return false;
    }

    private boolean search(int[] row, int target) {
        int left = 0, right = row.length - 1;
        // 对每行进行二分查找
        while (left <= right) {
            int mid = (right - left) / 2 + left;
            if (row[mid] == target)
                return true;
            else if (row[mid] < target)
                left = mid + 1;
            else
                right = mid - 1;
        }
        return false;
    }
}
